| Conditions | 1 |
| Paths | 1 |
| Total Lines | 116 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | $(function () { |
||
| 79 | window.setupMonthYearChart = function (id, datasets, labels, maxTotal) { |
||
| 80 | /** |
||
| 81 | * Namespaces that have been excluded from view via clickable |
||
| 82 | * labels above the chart. |
||
| 83 | * @type {Array} |
||
| 84 | */ |
||
| 85 | var excludedNamespaces = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Number of digits of the max month/year total. We want to keep this consistent |
||
| 89 | * for aesthetic reasons, even if the updated totals are fewer digits in size. |
||
| 90 | * @type {Number} |
||
| 91 | */ |
||
| 92 | var maxDigits = maxTotal.toString().length; |
||
| 93 | |||
| 94 | /** @type {Array} Labels for each namespace. */ |
||
| 95 | var namespaces = datasets.map(function (dataset) { |
||
| 96 | return dataset.label; |
||
| 97 | }); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Build the labels for the y-axis of the year/monthcount charts, |
||
| 101 | * which include the year/month and the total number of edits across |
||
| 102 | * all namespaces in that year/month. |
||
| 103 | */ |
||
| 104 | function getYAxisLabels() |
||
| 105 | { |
||
| 106 | var labelsAndTotals = {}; |
||
| 107 | datasets.forEach(function (namespace) { |
||
| 108 | if (excludedNamespaces.indexOf(namespace.label) !== -1) { |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 112 | namespace.data.forEach(function (count, index) { |
||
| 113 | if (!labelsAndTotals[labels[index]]) { |
||
| 114 | labelsAndTotals[labels[index]] = 0; |
||
| 115 | } |
||
| 116 | labelsAndTotals[labels[index]] += count; |
||
| 117 | }); |
||
| 118 | }); |
||
| 119 | |||
| 120 | // Format labels with totals next to them. This is a bit hacky, |
||
| 121 | // but it works! We use tabs (\t) to make the labels/totals |
||
| 122 | // for each namespace line up perfectly. |
||
| 123 | // The caveat is that we can't localize the numbers because |
||
| 124 | // the commas are not monospaced :( |
||
| 125 | return Object.keys(labelsAndTotals).map(function (year) { |
||
| 126 | var digitCount = labelsAndTotals[year].toString().length; |
||
| 127 | var numTabs = (maxDigits - digitCount) * 2; |
||
| 128 | |||
| 129 | // +5 for a bit of extra spacing. |
||
| 130 | return year + Array(numTabs + 5).join("\t") + |
||
| 131 | labelsAndTotals[year]; |
||
| 132 | }); |
||
| 133 | } |
||
| 134 | |||
| 135 | window[id + 'countsChart'] = new Chart($('#' + id + 'counts-canvas'), { |
||
| 136 | type: 'horizontalBar', |
||
| 137 | data: { |
||
| 138 | labels: getYAxisLabels(), |
||
| 139 | datasets: datasets |
||
| 140 | }, |
||
| 141 | options: { |
||
| 142 | tooltips: { |
||
| 143 | intersect: true, |
||
| 144 | callbacks: { |
||
| 145 | label: function (tooltip) { |
||
| 146 | return tooltip.xLabel.toLocaleString(); |
||
| 147 | }, |
||
| 148 | title: function (tooltip) { |
||
| 149 | var yLabel = tooltip[0].yLabel.replace(/\t.*/, ''); |
||
| 150 | return yLabel + ' - ' + namespaces[tooltip[0].datasetIndex]; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | }, |
||
| 154 | responsive: true, |
||
| 155 | maintainAspectRatio: false, |
||
| 156 | scales: { |
||
| 157 | xAxes: [{ |
||
| 158 | stacked: true, |
||
| 159 | ticks: { |
||
| 160 | beginAtZero: true, |
||
| 161 | callback: function (value) { |
||
| 162 | if (Math.floor(value) === value) { |
||
| 163 | return value.toLocaleString(); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | }], |
||
| 168 | yAxes: [{ |
||
| 169 | stacked: true |
||
| 170 | }] |
||
| 171 | }, |
||
| 172 | legend: { |
||
| 173 | // Happens when the user enables/disables a namespace via the |
||
| 174 | // labels above the chart. |
||
| 175 | onClick: function (e, legendItem) { |
||
| 176 | // Update totals, skipping over namespaces that have been excluded. |
||
| 177 | if (legendItem.hidden) { |
||
| 178 | excludedNamespaces = excludedNamespaces.filter(function (namespace) { |
||
| 179 | return namespace !== legendItem.text; |
||
| 180 | }); |
||
| 181 | } else { |
||
| 182 | excludedNamespaces.push(legendItem.text); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Update labels with the new totals. |
||
| 186 | window[id + 'countsChart'].config.data.labels = getYAxisLabels(); |
||
| 187 | |||
| 188 | // Yield to default onClick event, which re-renders the chart. |
||
| 189 | Chart.defaults.global.legend.onClick.call(this, e, legendItem); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | }); |
||
| 194 | } |
||
| 195 |